package com.apartmentManage.service.impl;

import com.apartmentManage.dao.AbsenceLogDao;
import com.apartmentManage.entity.AbsenceLog;
import com.apartmentManage.entity.IntoPersonnel;
import com.apartmentManage.service.AbsenceLogService;
import com.apartmentManage.service.IntoPersonnelService;
import com.apartmentManage.util.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class AbsenceLogServiceImpl implements AbsenceLogService {

    @Autowired
    private AbsenceLogDao absenceLogDao;

    @Autowired
    private IntoPersonnelService intoPersonnelService;

    @Override
    public boolean save(AbsenceLog absenceLog) {
        if (StringUntil.IsBlank(absenceLog.getSno())
        || StringUntil.IsBlank(absenceLog.getApartmentno())
        || StringUntil.IsBlank(absenceLog.getRoomno())
        ||StringUntil.IsBlank(absenceLog.getSname()))
            return false;
//        查看入住人员中是否有对应的登记的人员信息
        IntoPersonnel intopersonnel = intoPersonnelService.getByNo(absenceLog.getSno());
        if (intopersonnel==null)
            return false;
        if (absenceLog.getSno().equals(intopersonnel.getSno())
                && absenceLog.getApartmentno().equals(intopersonnel.getApartmentno())
        && absenceLog.getRoomno().equals(intopersonnel.getRoomno())
                && absenceLog.getSname().equals(intopersonnel.getSname())){
            long time = System.currentTimeMillis();
            absenceLog.setAbsence_date(new Date(time));
            return absenceLogDao.save(absenceLog)>0;
        }
            return false;

    }

    @Override
    public boolean update(AbsenceLog absenceLog) {
        if (StringUntil.IsBlank(absenceLog.getSno())
                || StringUntil.IsBlank(absenceLog.getApartmentno())
                || StringUntil.IsBlank(absenceLog.getRoomno())
                ||StringUntil.IsBlank(absenceLog.getSname())
        || StringUntil.IsBlank(absenceLog.getAbsence_date()))
            return false;
//        校验是更新的缺勤人员信息是否与入住人员信息相同
        IntoPersonnel intopersonnel = intoPersonnelService.getByNo(absenceLog.getSno());
        if (absenceLog.getApartmentno().equals(intopersonnel.getApartmentno())
                && absenceLog.getRoomno().equals(intopersonnel.getRoomno())
                && absenceLog.getSname().equals(intopersonnel.getSname())){
            return absenceLogDao.update(absenceLog)>0;
        }
     return false;
    }

    @Override
    public boolean delete(String sno) {
        return absenceLogDao.delete(sno)>0;
    }

    @Override
    public List<AbsenceLog> getAll() {
        return absenceLogDao.getAll();
    }

    @Override
    public AbsenceLog getByNo(String sno) {
        return absenceLogDao.getByNo(sno);
    }

    @Override
    public List<AbsenceLog> getByRequirement(AbsenceLog absenceLog) {
        return absenceLogDao.getByRequirement(absenceLog);
    }
}
